import 'package:flutter/material.dart';

class LocationSearchPage extends StatefulWidget {
  @override
  State createState() => _LocationSearchPageState();
}

class _LocationSearchPageState extends State<LocationSearchPage> {
  List<String> allData = [
    'Shanghai',
    'New York',
    'Tokyo',
    'Dubai',
    'Los Angeles',
    'London',
  ];
  List<String> filteredData = [];

  @override
  void initState() {
    super.initState();
    filteredData = allData;
  }

  void _filterData(String enteredKeyword) {
    List<String> results = [];
    if (enteredKeyword.isEmpty) {
      results = allData;
    } else {
      results = allData
          .where((item) =>
              item.toLowerCase().contains(enteredKeyword.toLowerCase()))
          .toList();
    }
    setState(() {
      filteredData = results;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('港口搜索'),
        backgroundColor: Color(0xffE22E4F),
      ),
      body: Column(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              onChanged: (value) => _filterData(value),
              decoration: InputDecoration(
                labelText: '搜索港口',
                suffixIcon: Icon(Icons.search),
              ),
            ),
          ),
          Expanded(
            child: ListView.builder(
              itemCount: filteredData.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(filteredData[index]),
                  onTap: () {
                    Navigator.pop(context, allData[index]); 
                  },
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}